/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "GenBankDlg.h"

#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>

const double	c_kilobyte = 1024.0;
const double	c_megabyte = c_kilobyte * c_kilobyte;
const double	c_gigabyte = c_kilobyte * c_megabyte;

/**
**/
GenBankDlg::GenBankDlg( QWidget *parent, Bio::GenBank *genBank ) : QDialog( parent ), m_genBank( genBank )
{
	setWindowTitle( tr( "Import GenBank files" ) );

	initLayout();
	updateTotal();
}

/**
**/
GenBankDlg::~GenBankDlg()
{
}

/**
**/
void GenBankDlg::accept()
{
	QDialog::accept();
}

/**
**/
void GenBankDlg::updateTotal()
{
	double	size = 0.0;

	for( int i = 0; i < Bio::GenBank::NumDivisions; i++ ){
		if( m_divBox[i]->isChecked() ){
			size += double( m_genBank->getDivisionSize( Bio::GenBank::Division( i ) ) );
		}
	}

	m_totalSizeLbl->setText( "Total: " + sizeToText( size ) );
}

/**
**/
void GenBankDlg::initLayout()
{
	QPushButton	*btn;
	QVBoxLayout	*layout = new QVBoxLayout;

	setLayout( layout );

	QLabel	*lbl = new QLabel( QString( "GenBank Release Number: %1" ).arg( m_genBank->getReleaseNumber() ), this );

	layout->addWidget( lbl );
	layout->addSpacing( 20 );

	for( int i = 0; i < Bio::GenBank::NumDivisions; i++ ){
		m_divBox[i] = new QCheckBox( getDivisionText( i ), this );
		connect( m_divBox[i], SIGNAL(stateChanged(int)), this, SLOT(updateTotal()) );
		layout->addWidget( m_divBox[i] );
	}

	layout->addSpacing( 20 );
	layout->addStretch( 100 );

	QHBoxLayout	*btnLayout = new QHBoxLayout;

	layout->addLayout( btnLayout );

	btnLayout->setMargin( 0 );

	m_totalSizeLbl = new QLabel( this );
	btnLayout->addWidget( m_totalSizeLbl );

	btnLayout->addStretch( 100 );

	btn = new QPushButton( tr( "Apply" ), this );
	connect( btn, SIGNAL(clicked()), this, SLOT(accept()) );
	btnLayout->addWidget( btn );

	btn = new QPushButton( tr( "Cancel" ), this );
	connect( btn, SIGNAL(clicked()), this, SLOT(reject()) );
	btnLayout->addWidget( btn );
}

/**
**/
QString GenBankDlg::getDivisionText( int which )
{
	Bio::GenBank::Division	div = Bio::GenBank::Division( which );
	QString					text = Bio::GenBank::getDivisionDescription( div );
	
	text += " (" + sizeToText( double( m_genBank->getDivisionSize( div ) ) ) + ")";

	return text;
}

/**
**/
QString GenBankDlg::sizeToText( double size )
{
	if( size > c_gigabyte )
		return QString( "%1 GB" ).arg( size / c_gigabyte, 0, 'f', 1 );

	if( size > c_megabyte )
		return QString( "%1 MB" ).arg( size / c_megabyte, 0, 'f', 1 );

	if( size > c_kilobyte )
		return QString( "%1 KB" ).arg( size / c_kilobyte, 0, 'f', 1 );

	return QString( "%1 Bytes" ).arg( int( size ) );
}
